<template>
	<view>
		<button class="avatar-wrapper" open-type="chooseAvatar" hover-class="none" @chooseavatar="onChooseAvatar">
			<view class="headimg flexcenter">
				<image :src="avatar_text?avatar_text:'../../static/image/system/weidenglu.png'" mode=""></image>
				<view class="xiangji"></view>
			</view>
		</button>
		
		<view class="ffffff mainpadding_top flexbetween" style="margin-top: 60rpx;">
			<view class="titletext hei_text bold">昵称</view>
			<input type="nickname" placeholder="请输入" style="text-align: right;" class="hei_text main_size bold" @blur="getnickname" v-model="nickname">
		</view>
		<view class="ffffff mainpadding_top flexbetween" style="margin-top: 60rpx;">
			<view class="titletext hei_text bold">手机号</view>
			<input type="number" placeholder="请输入" style="text-align: right;" class="hei_text main_size bold" v-model="mobile">
		</view>
		<view class="posibottom mainpadding" style="bottom: 0;">
			<view class="submitbtn" @click="submit">保存</view>
		</view>
	</view>
</template>

<script>
	const httpRequest = require("@/common/httpRequest.js")
	export default {
		data() {
			return {
				avatar:"",
				avatar_text:"",
				nickname:"",
				listid:"",
				mobile:"",
			}
		},
		onShow() {
			// this.init()
		},
		onLoad(options) {
			this.listid = options.id
		},
		methods: {
			getnickname(e){
				this.nickname = e.detail.value
			},
			onChooseAvatar(e) {
				this.uploadImageService(e.detail.avatarUrl)
			},
			uploadImageService(url) {
				let _this = this;
				httpRequest.uploadFile('/api/common/upload', url).then(res => {
					this.avatar = res.url
					this.avatar_text = res.fullurl
				}).catch(err => {});
			},
			submit(){
				if(this.avatar == ""){
					httpRequest.toast("请上传头像")
					return false
				}
				if(this.nickname == ""){
					httpRequest.toast("请输入昵称")
					return false
				}
				if(this.mobile == ""){
					httpRequest.toast("请输入手机号")
					return false
				}
				httpRequest.request('/api/user/register', 'GET', {
					nickname:this.nickname,
					avatar:this.avatar,
					mobile:this.mobile,
					third_id:this.listid,
				}, false, false, true).then(res => {
					if(res.code==1){
						uni.setStorageSync('token', res['data']['userinfo']['token']);
						uni.setStorageSync('user_id', res['data']['userinfo']['user_id']);
						
						httpRequest.toast("登录成功");
						setTimeout(() => {
							uni.reLaunch({
								url:"/pages/index/index"
							})
						}, 1000)
					}else{
						httpRequest.toast(res.msg);
					}
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			init() {
				httpRequest.request('/api/user/index', 'GET', {}, false, false, true).then(res => {
					this.avatar = res.data.avatar
					this.avatar_text = res.data.avatar_text
					this.nickname = res.data.nickname
				}).catch(err => {
					httpRequest.toast(err.msg);
				})
			},
			
		}
	}
</script>

<style lang="scss" scoped>
	.headimg {
		width: 180rpx;
		height: 180rpx;
		margin: 30rpx auto 0;
		padding-top: 30rpx;
		position: relative;

		image {
			width: 100%;
			height: 100%;
			border-radius: 50%;
		}

		.xiangji {
			width: 40rpx;
			height: 40rpx;
			position: absolute;
			bottom: 0;
			right: 0;
			background-size: 100% 100%;
			background-image: url('/static/image/system/avatarup.png');
		}
	}
</style>